package com.yx.listener;

import com.alibaba.fastjson.JSON;
import com.rabbitmq.client.Channel;
import com.yx.constatns.RabbitMQConstatns;
import com.yx.vo.Goods;
import com.yx.vo.Sms;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Classname GoodsESQueueListener
 * @Date 2021/07/12 19:50
 * @Author by yx
 * @Description TODO:
 */
@Component
@Slf4j
@Data
@ConfigurationProperties(prefix = "sms")
public class SmsQueueListener {

    private String host;
    private String path;
    private String appCode;


    @RabbitListener(queues = RabbitMQConstatns.SMS_QUEUE)
    public void GoodsTohtml(String json, Channel channel, Message message) throws IOException {
        //1.获取routingkey
        String routingKey = message.getMessageProperties().getReceivedRoutingKey();
        Map<String, String> headers = new HashMap<String, String>();
        Sms sms = JSON.parseObject(json, Sms.class);
        log.debug("发送短信:{}", sms);
//        log.debug("host:{}", host);
//        log.debug("path:{}", path);
//        log.debug("appCode:{}", appCode);

        switch (routingKey) {
            case "shop.sms.phone":

                //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
                headers.put("Authorization", "APPCODE " + appCode);
                Map<String, String> querys = new HashMap<String, String>();
                querys.put("mobile", sms.getPhone());
                querys.put("templateId", sms.getTemplateId());
                querys.put("value", sms.getCode());
                Map<String, String> bodys = new HashMap<String, String>();


                try {
                    /**
                     * 重要提示如下:
                     * HttpUtils请从
                     * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
                     * 下载
                     *
                     * 相应的依赖请参照
                     * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
                     */
//                    HttpResponse response = com.yx.utils.HttpUtils.doPost(host, path, "POST", headers, querys, bodys);
//                    System.out.println(EntityUtils.toString(response.getEntity()));
                    channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
                } catch (Exception e) {
                    log.error("验证码发送异常", e);
                    channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, false);
                }
                break;
            default:
                System.out.println("无此routingKey");
                break;
        }


    }
}
